package com.chushouya.product.service.admin.impl;

import com.chushouya.product.dto.admin.attribute.*;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.schema.PageResult;
import com.chushouya.product.dao.entity.AttributeItemEntity;
import com.chushouya.product.dao.entity.AttributeItemValueEntity;
import com.chushouya.product.dao.entity.CategoryEntity;
import com.chushouya.product.dao.repository.AttributeItemRepository;
import com.chushouya.product.dao.repository.AttributeItemValueRepository;
import com.chushouya.product.dao.repository.CategoryRepository;
//=====product.dto.admin.attribute.*;
import com.chushouya.product.dto.admin.attribute.*;
import com.chushouya.product.service.admin.AttributeItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class AttributeItemServiceImpl implements AttributeItemService {

    @Resource
    private AttributeItemRepository attributeItemRepository;

    @Resource
    private AttributeItemValueRepository attributeItemValueRepository;

    @Resource
    private CategoryRepository categoryRepository;


    private CategoryEntity getCategory(Long categoryId) {
        Check.notNull(categoryId, "categoryId 不能为空");
        CategoryEntity category = categoryRepository.selectByPrimaryKey(categoryId);
        Check.notNull(category, "分类不存在");
        return category;
    }

    @Override
    public PageResult<AttributeItemListDTO> selectAttributeItemPageResult(AttributeItemQuery query) {
        PageResult<AttributeItemEntity> pageResult = attributeItemRepository.selectPageResult(query);
        return PageResult.of(pageResult, AttributeItemListDTO.class);
    }

    @Override
    public List<AttributeItemListDTO> selectAttributeItemList(AttributeItemQuery query) {
        List<AttributeItemEntity> list = attributeItemRepository.selectList(query);
        return Beans.copyList(list, AttributeItemListDTO.class);
    }

    @Override
    public AttributeItemDetailDTO getAttributeItemDetail(Long attrItemId) {
        Check.notNull(attrItemId, "attrItemId 不能为空");
        AttributeItemEntity entity = attributeItemRepository.selectByPrimaryKey(attrItemId);
        return Beans.copy(entity, AttributeItemDetailDTO.class);
    }

    @Override
    public Long addAttributeItem(AttributeItemSaveDTO attributeItem) {
        Check.notEmpty(attributeItem.getAttrName(), "SKU属性名称不能为空");
        Check.notNull(attributeItem.getCategoryId(), "分类ID不能为空");
        CategoryEntity category = getCategory(attributeItem.getCategoryId());
        AttributeItemEntity entity = Beans.copy(attributeItem, AttributeItemEntity.class);
        entity.setCategoryName(category.getCategoryName());
        entity.setCreateTime(Dates.getTimeNow());

        attributeItemRepository.insertSelective(entity);
        return entity.getAttrItemId();
    }

    @Override
    public Long editAttributeItem(AttributeItemUpdateDTO attributeItem) {
        Check.notNull(attributeItem.getAttrItemId(), "attrItemId 不能为空");
        Check.notEmpty(attributeItem.getAttrName(), "SKU属性名称不能为空");
        
        AttributeItemEntity existEntity = attributeItemRepository.selectByPrimaryKey(attributeItem.getAttrItemId());
        Check.notNull(existEntity, "SKU属性不存在");
        CategoryEntity category = getCategory(attributeItem.getCategoryId());
        
        AttributeItemEntity updateEntity = Beans.copy(attributeItem, AttributeItemEntity.class);
        updateEntity.setCategoryName(category.getCategoryName());
        attributeItemRepository.updateByPrimaryKeySelective(updateEntity);
        return updateEntity.getAttrItemId();
    }

    @Override
    public void removeAttributeItem(Long attrItemId) {
        Check.notNull(attrItemId, "attrItemId不能为空");
        AttributeItemEntity entity = attributeItemRepository.selectByPrimaryKey(attrItemId);
        Check.notNull(entity, "SKU属性不存在");
        AttributeItemValueQuery query = new AttributeItemValueQuery();
        query.setAttrItemId(attrItemId);
        AttributeItemValueEntity attributeItemValue = attributeItemValueRepository.selectOne(query);
        if (Objects.nonNull(attributeItemValue)) {
            throw Ex.business("该属性下存在属性值，不能删除");
        }
        attributeItemRepository.deleteByPrimaryKey(attrItemId);
    }

    @Override
    public List<AttributeItemListDTO> selectAttributeItemListByCategoryId(Long categoryId) {
        List<AttributeItemEntity> list = attributeItemRepository.selectListByCategoryId(categoryId);
        return Beans.copyList(list, AttributeItemListDTO.class);
    }
}
